package com.llsydn.data.network;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.llsydn.hello.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 远程服务器存储
 * @description 请填写相关描述
 * @date 2018/9/26 16:42
 */
public class NetworkActivity extends AppCompatActivity {
    private EditText et_network_url;
    private EditText et_network_result;
    private RequestQueue queue;

    /**
     * 对于联网的APP来说, 可能需要通过请求向服务器提交请求数据, 也可能需要从服务器端获取数据显示
     * 如何编码实现客户端与服务器端的交互呢?
     * 1.JDK内置的原生API      HttpUrlConnection
     * 2.Android内置的包装API  HttpClient    浏览器
     * 3.异步网络请求框架      Volley Xutils
     * 注意:
     * 访问网络, 需要声明权限: android.permission.INTERNET
     * 访问网络的程序必须在分线程执行
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);

        //初始化
        et_network_url = findViewById(R.id.et_network_url);
        et_network_result = findViewById(R.id.et_network_result);

        //创建一个请求队列
        queue = Volley.newRequestQueue(this);
    }

    /**
     * 使用HttpConnection：Get请求
     *
     * @param view
     */
    public void testConnectionGet(View view) {
        //1.显示ProgressDialog
        ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
        //2.启动分线程
        new Thread() {
            //3.在分线程，发送请求，得到相应数据
            public void run() {
                try {
                    //1.得到path，带上参数
                    String path = et_network_url.getText().toString();
                    //2.创建URL对象
                    URL url = new URL(path);
                    //3.打开连接，得到HttpURLConnection对象
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //4.设置请求方式，连接超时，读取数据超时
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(100000);
                    connection.setReadTimeout(100000);
                    //5.连接服务器
                    connection.connect();
                    //6.发请求，得到响应数据
                    //1.得到响应码，必须是200才继续
                    int responseCode = connection.getResponseCode();
                    if (200 == responseCode) {
                        //2.得到InputStream，并读取成String
                        InputStream is = connection.getInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len = -1;
                        while ((len = is.read(buffer)) != -1) {
                            baos.write(buffer, 0, len);
                        }
                        String result = baos.toString();
                        baos.close();
                        is.close();
                        //4.在主线程，显示得到的结果，移除dialog
                        runOnUiThread(new Runnable() {
                            //Runnable的run方法在主线程执行
                            @Override
                            public void run() {
                                et_network_result.setText(result);
                                dialog.dismiss();
                            }
                        });
                    }
                    //7.关闭连接
                    connection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                    dialog.dismiss();
                }
            }
        }.start();
    }

    /**
     * 使用HttpConnection：Post请求
     *
     * @param view
     */
    public void testConnectionPost(View view) {
        //1.显示ProgressDialog
        ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
        //2.启动分线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //3.在分线程，发送请求，得到相应数据
                try {
                    //1.得到path，带上参数
                    String path = et_network_url.getText().toString();
                    //2.创建URL对象
                    URL url = new URL(path);
                    //3.打开连接，得到HttpURLConnection对象
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //4.设置请求方式，连接超时，读取数据超时
                    connection.setRequestMethod("POST");
                    connection.setConnectTimeout(100000);
                    connection.setReadTimeout(100000);
                    //5.连接服务器
                    connection.connect();
                    //6.发请求，得到响应数据
                    //得到输出流，写请求体
                    OutputStream os = connection.getOutputStream();
                    String data = "name=Tom&age=11";
                    os.write(data.getBytes("utf-8"));

                    //1.得到响应码，必须是200才继续
                    int responseCode = connection.getResponseCode();
                    if (200 == responseCode) {
                        //2.得到InputStream，并读取成String
                        InputStream is = connection.getInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len = -1;
                        while ((len = is.read(buffer)) != -1) {
                            baos.write(buffer, 0, len);
                        }
                        String result = baos.toString();
                        baos.close();
                        is.close();
                        //4.在主线程，显示得到的结果，移除dialog
                        runOnUiThread(new Runnable() {
                            //Runnable的run方法在主线程执行
                            @Override
                            public void run() {
                                et_network_result.setText(result);
                                dialog.dismiss();
                            }
                        });
                    }
                    os.close();
                    //7.关闭连接
                    connection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                    dialog.dismiss();
                }
            }
        }).start();
    }

    /*
     * 使用httpClient提交get请求
     */
    public void testClientGet(View v) {
        //1. 显示ProgressDialog
        final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
        //2. 启动分线程
        new Thread() {
            //3. 在分线程, 发送请求, 得到响应数据
            public void run() {
                try {
                    //1). 得到path, 并带上参数name=Tom1&age=11
                    String path = et_network_url.getText().toString() + "?name=Tom3&age=13";

                    //2). 创建HttpClient对象
                    HttpClient httpClient = new DefaultHttpClient();
                    //3). 设置超时
                    HttpParams params = httpClient.getParams();
                    HttpConnectionParams.setConnectionTimeout(params, 5000);
                    HttpConnectionParams.setSoTimeout(params, 5000);
                    //4). 创建请求对象
                    HttpGet request = new HttpGet(path);
                    //5). 执行请求对象, 得到响应对象
                    HttpResponse response = httpClient.execute(request);

                    int statusCode = response.getStatusLine().getStatusCode();
                    if (statusCode == 200) {
                        //6). 得到响应体文本
                        HttpEntity entity = response.getEntity();
                        final String result = EntityUtils.toString(entity);
                        //4. 要主线程, 显示数据, 移除dialog
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                et_network_result.setText(result);
                                dialog.dismiss();
                            }
                        });
                    }
                    //7). 断开连接
                    httpClient.getConnectionManager().shutdown();
                } catch (Exception e) {
                    e.printStackTrace();
                    //如果出了异常要移除dialog
                    dialog.dismiss();
                }
            }
        }.start();
    }

    /*
     * 使用httpClient提交post请求
     */
    public void testClientPost(View v) {
        //1. 显示ProgressDialog
        final ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");
        //2. 启动分线程
        new Thread() {
            //3. 在分线程, 发送请求, 得到响应数据
            public void run() {
                try {
                    //1). 得到path
                    String path = et_network_url.getText().toString();

                    //2). 创建HttpClient对象
                    HttpClient httpClient = new DefaultHttpClient();
                    //3). 设置超时
                    HttpParams params = httpClient.getParams();
                    HttpConnectionParams.setConnectionTimeout(params, 5000);
                    HttpConnectionParams.setSoTimeout(params, 5000);
                    //4). 创建请求对象
                    HttpPost request = new HttpPost(path);
                    //设置请求体
                    List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
                    parameters.add(new BasicNameValuePair("name", "Tom4"));
                    parameters.add(new BasicNameValuePair("age", "14"));
                    HttpEntity entity = new UrlEncodedFormEntity(parameters);
                    request.setEntity(entity);

                    //5). 执行请求对象, 得到响应对象
                    HttpResponse response = httpClient.execute(request);

                    int statusCode = response.getStatusLine().getStatusCode();
                    if (statusCode == 200) {
                        //6). 得到响应体文本
                        entity = response.getEntity();
                        final String result = EntityUtils.toString(entity);
                        //4. 要主线程, 显示数据, 移除dialog
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                et_network_result.setText(result);
                                dialog.dismiss();
                            }
                        });
                    }
                    //7). 断开连接
                    httpClient.getConnectionManager().shutdown();
                } catch (Exception e) {
                    e.printStackTrace();
                    //如果出了异常要移除dialog
                    dialog.dismiss();
                }
            }
        }.start();
    }

    /*
     * 使用Volley提交get请求
     */
	/*
	 1. 创建请求队列对象(一次)
	 2. 创建请求对象StringRequest
	 3. 将请求添加到队列中
	 */
    public void testVolleyGet(View v) {
        ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");

        //创建请求对象StringRequest
        String path = et_network_url.getText().toString() + "?name=Tom5&age=15";
        StringRequest request = new StringRequest(path, (response) -> {
            //在主线程执行
            et_network_result.setText(response);
            dialog.dismiss();
        }, null);
        //将请求添加到队列中
        queue.add(request);
    }

    /*
     * 使用Volley提交post请求
     */
    public void testVolleyPost(View v) {
        ProgressDialog dialog = ProgressDialog.show(this, null, "正在请求中...");

        //创建请求对象StringRequest
        String path = et_network_url.getText().toString();
        StringRequest request = new StringRequest(Method.POST, path, (response) -> {
            et_network_result.setText(response);
            dialog.dismiss();
        }, null) {
            //重写此方法返回参数的map作为请求体
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("name", "Tom6");
                map.put("age", "16");
                return map;
            }
        };
        //将请求添加到队列中
        queue.add(request);
    }

}
